var http = require("http");
var socketio = require("socket.io");
var session = require("express-session");
var sharedsession = require('express-socket.io-session');

var config = require('./config')
var logger = require('./logger')
var sounddb = require('./db/sound.db')
var musicdb = require('./db/music.db')
var userdb = require('./db/user.db')
var orderdb = require('./db/order.db')
var emit = require('./emit');

var impt = require('./socket/manager/import')
var getorderlist = require("./socket/browser/getorderlist");
var play = require("./socket/play");
var search = require("./socket/search");
var removefile = require("./socket/manager/removefile");
var fileaddtag = require("./socket/manager/fileaddtag");
var fileremovetag = require("./socket/manager/fileremovetag");
var gettaglist = require("./socket/manager/gettaglist");
var newtag = require("./socket/manager/newtag");
var deletetag = require("./socket/manager/deletetag");
var download = require("./socket/manager/download");
var clean = require("./socket/manager/clean");
var alluser = require("./socket/manager/alluser");
var newuser = require("./socket/manager/newuser");
var activeuser = require("./socket/manager/activeuser");
var total = require("./socket/total");
var star = require("./socket/browser/star");
var addcartitem = require("./socket/browser/addcartitem");
var removecartitem = require("./socket/browser/removecartitem");
var getcartlist = require("./socket/browser/getcartlist");
var createorder = require("./socket/browser/createorder");
var allorder = require("./socket/manager/allorder");
var finishorder = require("./socket/manager/finishorder");
var removeorderitem = require("./socket/browser/removeorderitem");
var orderdetail = require("./socket/manager/orderdetail");

function onsocket(socket, key, func) {
  socket.on(key, function (data) {
    logger.log('onsocket',socket.handshake.cookies.authorized,key);
    func(socket, data)
  });
}


function init(app) {
  var server = http.createServer(app);
  var io = socketio.listen(server);
  var s = session({
    secret : config.sessionSecret,
    resave: true,
    saveUninitialized:true
  });
  app.use(s);

  io.use(sharedsession(s,{autoSave:true}));
  // //Debugging express
  // app.use('*', function(req, res, next) {
  //   logger.log('Express `req.session` data is %j.', req.session);
  //   next();
  // });
  // // Debugging io
  // io.use(function(socket, next) {
  //   logger.log('use socket.handshake session data is %j.', socket.handshake.session);
  //   next();
  // });

  io.on("connection",function(socket){
    emit.send(socket,'sessiondata', socket.handshake.session);

    onsocket(socket, 'play', play.onsocket);
    onsocket(socket, 'totalcount', total.onsocket);
    onsocket(socket, 'import', impt.onsocket);
    onsocket(socket, 'getorderlist', getorderlist.onsocket);
    onsocket(socket, 'search', search.onsocket);
    onsocket(socket, 'removefile', removefile.onsocket);
    onsocket(socket, 'fileaddtag', fileaddtag.onsocket);
    onsocket(socket, 'fileremovetag', fileremovetag.onsocket);
    onsocket(socket, 'gettaglist', gettaglist.onsocket);
    onsocket(socket, 'newtag', newtag.onsocket);
    onsocket(socket, 'deletetag', deletetag.onsocket);
    onsocket(socket, 'download', download.onsocket);
    onsocket(socket, 'total', total.onsocket);
    onsocket(socket, 'star', star.onsocket);
    onsocket(socket, 'clean', clean.onsocket);
    onsocket(socket, 'alluser', alluser.onsocket);
    onsocket(socket, 'newuser', newuser.onsocket);
    onsocket(socket, 'activeuser', activeuser.onsocket);
    onsocket(socket, 'addcartitem', addcartitem.onsocket);
    onsocket(socket, 'removecartitem', removecartitem.onsocket);
    onsocket(socket, 'getcartlist', getcartlist.onsocket);
    onsocket(socket, 'createorder', createorder.onsocket);    
    onsocket(socket, 'allorder', allorder.onsocket);
    onsocket(socket, 'finishorder', finishorder.onsocket);
    onsocket(socket, 'removeorderitem', removeorderitem.onsocket);
    onsocket(socket, 'orderdetail', orderdetail.onsocket)
  });
  server.listen(config.websocketPort);

  sounddb.connect(function (err) {
    if (err) {
      logger.log('connect sound database error',err)
    }
  });

  musicdb.connect(function (err) {
    if (err) {
      logger.log('connect music database error',err)
    }
  });
  
  userdb.connect(function (err) {
    if (err) {
      logger.log('connect user database error',err)
    }
  });
  
  orderdb.connect(function (err) {
    if (err) {
      logger.log('connect order database error',err)
    }
  });
  

  if (config.debug) {
    //impt.onsocket();
    var data = { key: 'effect', order: 'name', index: 0, count: 20 }
    search.onsocket(null, data);
    // var data = { key: 'effect', order: 'name', index: 20, count: 20 }
    // search.onsocket(null, data);
    // var data = { key: 'effect', order: 'name', index: 30, count: 20 }
    // search.onsocket(null, data);
  }
}

module.exports = {
  init
}
